package com.kg.sdk.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kg.sdk.util.Helper;


public class TCMLoadInputLayout extends FrameLayout implements View.OnClickListener , TextWatcher {
    private int inputStyle = TEXT;
    public final static int TEXT = 0;
    public final static int PASSOWRD = 1;
    public final static int CHECK = 2;
    public final static int NUMBER = 3;
    public final static int PHONE = 4;
    private static final int BTNId = 1000;
    private static final int LEFTID = 1001;
    protected static final int DEFAULT_SIZE = 15;
    private boolean isShowPassword = false;
    protected TextView btnRight;
    private View rightView;
    protected EditText etInput;
    private ImageView img;
    public TCMLoadInputLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        int layoutId = Helper.getLayoutResId(context,"lib_login_layout");
        LayoutInflater.from(context).inflate(layoutId,this);
    }

    public void onFinishInflate(){
        super.onFinishInflate();
        btnRight = findViewById(Helper.getIdResId(getContext(),"tvRight"));
        rightView = findViewById(Helper.getIdResId(getContext(),"frClose"));
        etInput = findViewById(Helper.getIdResId(getContext(),"edit"));
        img = findViewById(Helper.getIdResId(getContext(),"img"));
      //  btnRight.setOnClickListener(this);
        rightView.setOnClickListener(this);
        etInput.addTextChangedListener(this);
        setRighShow(false);
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        if(inputStyle == TEXT || inputStyle == PHONE){
            int len = editable.length();
            if(len > 0){
                setRighShow(true);
            }else{
                //   btnRight.setVisibility(View.GONE);
                setRighShow(false);
            }
        }
    }
    private void setRighShow(boolean is){

        if(inputStyle == CHECK){
            if(btnRight != null){
                if(is){
                    btnRight.setVisibility(View.VISIBLE);
                }else{
                    btnRight.setVisibility(View.GONE);
                }

            }
        }else{
            if(rightView != null){
                if(is){
                    rightView.setVisibility(View.VISIBLE);
                }else{
                    rightView.setVisibility(View.GONE);
                }
            }
        }


    }
    public void setBtnRightText(String text){
        if(btnRight != null){
            btnRight.setText(text);
        }
    }

    public void setBtnRightEnable(boolean is){
        if(btnRight != null){
            btnRight.setEnabled(is);
        }
        if(rightView != null){
            rightView.setEnabled(is);
        }
    }

    public void setInputStyle(int style){
        inputStyle = style;
        switch (inputStyle){
            case TEXT:
                etInput.setInputType(InputType.TYPE_CLASS_TEXT);

                // btnRight.setVisibility(View.GONE);
                setRighShow(false);
                break;
            case PHONE:
                etInput.setInputType(InputType.TYPE_CLASS_PHONE);

                // btnRight.setVisibility(View.GONE);
                setRighShow(false);
                break;
            case PASSOWRD:
                etInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                //   btnRight.setVisibility(View.GONE);
                break;
            case CHECK:
                etInput.setInputType(InputType.TYPE_CLASS_TEXT);
                break;
            case NUMBER:
                etInput.setInputType(InputType.TYPE_CLASS_NUMBER);
                break;
        }
        if(inputStyle == TEXT || inputStyle == PASSOWRD || inputStyle == PHONE){

        }else if(inputStyle == CHECK){
            rightView.setVisibility(View.GONE);
            btnRight.setVisibility(View.VISIBLE);
        }
    }

    public void clearInputText(){
        etInput.setText("");
    }

    public void setPasswordShow(){
        if(!isShowPassword){
            isShowPassword = true;
            etInput.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }else{
            isShowPassword = false;
            etInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }
    }
    public String getInputText(){
        if(etInput != null){
            return etInput.getText().toString();
        }
        return "";
    }

    public void setBtnRightClickListener(OnClickListener listener){
        if(btnRight != null){
            btnRight.setOnClickListener(listener);
        }
        if(rightView != null){
            rightView.setOnClickListener(this);
        }
    }

    public void setInputText(String text){
        if(etInput!= null){
            etInput.setText(text);
        }
    }
    @Override
    public void onClick(View view) {
        switch (inputStyle){
            case TEXT:
                clearInputText();
                break;
            case PHONE:
                clearInputText();
                break;
            case PASSOWRD:
                setPasswordShow();
                break;
            case CHECK:
                break;
        }
    }

    public void setHit(String strId){
        String text = getContext().getString(Helper.getStringResId(getContext(),strId));
        etInput.setHint(text);
    }

    public void setImgDrawable(String drawableStr){
        img.setImageResource(Helper.getImageResId(getContext(),drawableStr));
    }

}
